/**
 * Created by lewis on 2017/7/24.
 */

/***********************json constants*********************/
//社保类型
var insuranceDropdownAsJson =
    [{
        "id": 0,
        "text": "养老保险",
        "selected": true
    }, {
        "id": 1,
        "text": "医疗保险"
    }, {
        "id": 2,
        "text": "工伤保险"
    }, {
        "id": 3,
        "text": "生育保险"
    }, {
        "id": 4,
        "text": "失业保险"
    }, {
        "id": 5,
        "text": "公积金"
    }, {
        "id": 6,
        "text": "残疾金"
    }, {
        "id": 7,
        "text": "大病医疗"
    }, {
        "id": 8,
        "text": "补充公积金"
    }
    ];

var householdDropdownAsJson =
    [{
        "id": 0,
        "text": "城镇户口",
        "selected": true

    }, {
        "id": 1,
        "text": "农村户口"

    }, {
        "id": 2,
        "text": "不限制"

    }];


var calcRegulationAsJson =
    [
        {
            "id": 0,
            "text": "见分进角",
            "selected": true
        }, {
        "id": 1,
        "text": "见角进元"
    }, {
        "id": 2,
        "text": "四舍五入"
    }, {
        "id": 3,
        "text": "向上取整"

    }, {
        "id": 4,
        "text": "向下取整"
    }];


var isParticipateInsuranceAsJson =
    [
        {
            "id": 0,
            "text": "未参加过本地社保",
            "selected": true
        }, {
        "id": 1,
        "text": "已参加过本地社保"
    }];
//户口类型
/***********************json constants*********************/


var insuranceDropdownForSearch = function () {
    dropdownWithLocalData('insuranceTypeForSearch', insuranceDropdownAsJson);
};

var insuranceDropdownForAdd = function () {
    dropdownWithLocalData('insuranceTypeForAdd', insuranceDropdownAsJson);
};

var householdDropdownForSearch = function () {
    dropdownWithLocalData('householdTypeForSearch', householdDropdownAsJson);

};

var householdDropdownForInput = function () {
    dropdownWithLocalData('householdTypeForInput', householdDropdownAsJson);
};

var householdDropdownForAdd = function () {
    dropdownWithLocalData('householdTypeForAdd', householdDropdownAsJson);
};

var householdDropdownForUpdate = function () {
    dropdownWithLocalData('householdTypeForUpdate', householdDropdownAsJson);
};

var calcRegulationDropdownForAdd = function () {
    dropdownWithLocalData('calculationRegularForAdd', calcRegulationAsJson);
};


var isParticipateInsuranceForAdd = function () {
    dropdownWithLocalData('typeForAdd', isParticipateInsuranceAsJson);
};

/***********************dropdown loader*********************/






function dropdownWithLocalData(idSelector, jsonData) {
    var idSelected = $('#' + idSelector);
    idSelected.combobox(
        "loadData", jsonData
    );
}




var provinceDropDown = function () {
    $('#provinceSelector').combobox({
        loader: function (param, success, error) {
            $.ajax({
                url: '/address/getProvinces',
                type: 'POST',
                data: {
                    enterpriseName: param
                },
                dataType: 'json',
                success: function (data) {
                    if (data.success) {
                        var items = $.map(data.dataWrapper.provinces, function (item) {
                            return {
                                id: item.id,
                                province: item.province
                            };
                        });
                        success(items);
                    }
                },

                error: function (xml, text, msg) {
                    error.apply(this, arguments);
                }
            });
        },
        onSelect: function (rec) {
            $('#citySelector').combobox('clear');
            var url = "/address/getCitiesByProvinceId?provinceId=" + rec.id;
            cityDropdown(url, 'citySelector');

        }

    });
};


var provinceDropDownForAdd = function () {
    $('#provinceDropDownForAdd').combobox({
        loader: function (param, success, error) {
            $.ajax({
                url: '/address/getProvinces',
                type: 'POST',
                data: {
                    enterpriseName: param
                },
                dataType: 'json',
                success: function (data) {
                    if (data.success) {
                        var items = $.map(data.dataWrapper.provinces, function (item) {
                            return {
                                id: item.id,
                                province: item.province
                            };
                        });
                        success(items);
                    }
                },

                error: function (xml, text, msg) {
                    error.apply(this, arguments);
                }
            });
        },
        onSelect: function (rec) {
            $('#cityDropDownForAdd').combobox('clear');
            var url = "/address/getCitiesByProvinceId?provinceId=" + rec.id;
            cityDropdown(url, 'cityDropDownForAdd');

        }

    });
};


var provinceDropDownForUpdate = function () {
    $('#provinceDropDownForUpdate').combobox({
        loader: function (param, success, error) {
            $.ajax({
                url: '/address/getProvinces',
                type: 'POST',
                data: {
                    enterpriseName: param
                },
                dataType: 'json',
                success: function (data) {
                    if (data.success) {
                        var items = $.map(data.dataWrapper.provinces, function (item) {
                            return {
                                id: item.id,
                                province: item.province
                            };
                        });
                        success(items);
                    }
                },

                error: function (xml, text, msg) {
                    error.apply(this, arguments);
                }
            });
        },
        onSelect: function (rec) {
            $('#cityDropDownForUpdate').combobox('clear');
            var url = "/address/getCitiesByProvinceId?provinceId=" + rec.id;
            cityDropdown(url, 'cityDropDownForUpdate');

        }

    });
};


var provinceDropDownForAdd = function () {
    $('#provinceDropDownForAdd').combobox({
        loader: function (param, success, error) {
            $.ajax({
                url: '/address/getProvinces',
                type: 'POST',
                data: {
                    enterpriseName: param
                },
                dataType: 'json',
                success: function (data) {
                    if (data.success) {
                        var items = $.map(data.dataWrapper.provinces, function (item) {
                            return {
                                id: item.id,
                                province: item.province
                            };
                        });
                        success(items);
                    }
                },

                error: function (xml, text, msg) {
                    error.apply(this, arguments);
                }
            });
        },
        onSelect: function (rec) {
            $('#cityDropDownForAdd').combobox('clear');
            var url = "/address/getCitiesByProvinceId?provinceId=" + rec.id;
            cityDropdown(url, 'cityDropDownForAdd');

        }

    });
};

//二级联动for dialog

var provinceDropDownForDialog = function () {
    $('#provinceDropDownForDialog').combobox({
        loader: function (param, success, error) {
            $.ajax({
                url: '/address/getProvinces',
                type: 'POST',
                dataType: 'json',
                success: function (data) {
                    if (data.success) {
                        var items = $.map(data.dataWrapper.provinces, function (item) {
                            return {
                                id: item.id,
                                province: item.province
                            };
                        });
                        success(items);
                    }
                },

                error: function (xml, text, msg) {
                    error.apply(this, arguments);
                }
            });
        },
        onSelect: function (rec) {
            $('#cityDropDownForDialog').combobox('clear');
            var url = "/address/getCitiesByProvinceId?provinceId=" + rec.id;
            cityDropdown(url, 'cityDropDownForDialog');

        }

    });
};

var cityDropdown = function (url, cityIdSelector) {
    $('#' + cityIdSelector).combobox({
        loader: function (param, success, error) {
            $.ajax({
                url: url,
                dataType: 'json',
                success: function (data) {
                    if (data.success) {
                        var items = $.map(data.dataWrapper.citiesGroupByProvince, function (item) {
                            return {
                                cityId: item.cityId,
                                city: item.city,
                                isPopularCity: item.popularCity
                            };
                        });
                        success(items);
                    }

                },
                error: function (xml, text, msg) {
                    error.apply(this, arguments);
                }
            });
        },
        onLoadSuccess: function (data) { //加载完成后,设置选中第一项
            if (data.length > 0) {
                var i = 0;
                for (; i < data.length; i++) {
                    var item = data[i];
                    if (item.isPopularCity) {
                        $(this).combobox("select", item['cityId']);
                    }
                }

            }
        }
    });
};


var commonCityDropDown = function () {
    $('#paymentPositionForAdd').combobox({
        loader: function (param, success, error) {
            $.ajax({
                url: '/address/getCities',
                dataType: 'json',
                success: function (data) {
                    if (data.success) {
                        var items = $.map(data.dataWrapper.cities, function (item) {
                            return {
                                cityId: item.cityId,
                                city: item.city,
                                isPopularCity: item.popularCity
                            };
                        });
                        success(items);
                    }

                },
                error: function (xml, text, msg) {
                    error.apply(this, arguments);
                }
            });
        }
    });
};



var serviceTemplateDropDownForAdd = function () {
    $('#serviceTemplateDropDownForAdd').combobox({
        loader: function (param, success, error) {
            $.ajax({
                url: '/address/getProvinces',
                type: 'POST',
                data: {
                    enterpriseName: param
                },
                dataType: 'json',
                success: function (data) {
                    if (data.success) {
                        var items = $.map(data.dataWrapper.provinces, function (item) {
                            return {
                                id: item.id,
                                province: item.province
                            };
                        });
                        success(items);
                    }
                },

                error: function (xml, text, msg) {
                    error.apply(this, arguments);
                }
            });
        },
        onSelect: function (rec) {
            $('#cityDropDownForAdd').combobox('clear');
            var url = "/address/getCitiesByProvinceId?provinceId=" + rec.id;
            cityDropdown(url, 'cityDropDownForAdd');

        }

    });
};